/*
 * Created on May 10, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package ro.xblue.translator;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.DBWrapable;
import ro.gateway.aida.db.PersistenceToken;
/**
 * @author mihai e-mail: mihai@ro-gateway.org
 */
public class TrnHarvestLocationDB extends DBPersistenceManager implements DBWrapable {
	private TrnHarvestLocationDB(PersistenceToken token) {
		super(token);
	}
	public static TrnHarvestLocationDB getManager(PersistenceToken token) {
		return new TrnHarvestLocationDB(token);
	}
	
	public void markPerformed(TrnHarvestLocation thl) throws SQLException {
		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(MARK_PERFORMED);

			ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
			ps.setInt(2, thl.getId());
			ps.executeUpdate();
			ps.close();
		} finally {
			con.close();
		}
	}
	public void update(TrnHarvestLocation thl) throws SQLException {
		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(UPDATE);
			ps.setString(1, thl.getName());
			ps.setString(2, thl.getBaseURL());
			ps.setString(3, thl.getLang());
			ps.setInt(4, thl.getId());
			ps.executeUpdate();
			ps.close();
		} finally {
			con.close();
		}
	}
	public void insert(TrnHarvestLocation thl) throws SQLException {
		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(INSERT);
			ps.setString(1, thl.getName());
			ps.setString(2, thl.getBaseURL());
			ps.setString(3, thl.getLang());
			ps.executeUpdate();
			ps.close();
		} finally {
			con.close();
		}
	}

	public void delete (Object o) throws SQLException {
		TrnHarvestLocation thl = (TrnHarvestLocation) o ;
		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(DELETE);
			ps.setInt(1,thl.getId());
			ps.executeUpdate();
			ps.close();
		} finally {
			con.close();
		}
	}

	
	public Object get(Object o) throws SQLException {
		TrnHarvestLocation thl = (TrnHarvestLocation) o ;
		if(thl.getId()!=0) return getById(thl.getId());
		return getList();
	}
	
	public void synchronize(Object o) throws SQLException {
		TrnHarvestLocation thl = (TrnHarvestLocation) o ;
		if(thl.isMarkPerformed()) markPerformed(thl); else
		if(thl.getId()==0) insert(thl);
		else
			update(thl);
	}
	
	public TrnHarvestLocation getById(int id) throws SQLException {
		Connection con = getConnection();
		TrnHarvestLocation thl = null;
		try {
			PreparedStatement ps = con.prepareStatement(GET_BY_ID);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				thl = new TrnHarvestLocation();
				thl.setId(id);
				thl.setName(rs.getString("name"));
				thl.setBaseURL(rs.getString("baseurl"));
				thl.setLang(rs.getString("lang"));
				thl.setPerformed(rs.getTimestamp("performed"));
			}
			rs.close();
			ps.close();
		} finally {
			con.close();
		}
		return thl;
	}
	
	
	public List getList() throws SQLException {
		Connection con = getConnection();
		TrnHarvestLocation thl = null;
		List result = new ArrayList();
		try {
			PreparedStatement ps = con.prepareStatement(GET_LIST);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				thl = new TrnHarvestLocation();
				thl.setId(rs.getInt("id"));
				thl.setName(rs.getString("name"));
				thl.setBaseURL(rs.getString("baseurl"));
				thl.setLang(rs.getString("lang"));
				thl.setPerformed(rs.getTimestamp("performed"));
				result.add(thl);
			}
			rs.close();
			ps.close();
		} finally {
			con.close();
		}
		return result;
	}
	private static final String GET_LIST = "SELECT id,name,baseurl,lang,performed FROM trn_harvest_locations ORDER BY id ";
	private static final String GET_BY_ID = "SELECT name,baseurl,lang,performed FROM trn_harvest_locations "
			+ "WHERE id=?";
	private static final String INSERT = "INSERT INTO trn_harvest_locations (name,baseurl,lang) VALUES (?,?,?)";
	private static final String UPDATE = "UPDATE trn_harvest_locations SET name=?,baseurl=?,lang=? WHERE id=?";
	private static final String MARK_PERFORMED = "UPDATE trn_harvest_locations SET performed=? WHERE id=?";
	private static final String DELETE = "DELETE FROM trn_harvest_locations WHERE id=?";
}
